package com.yunxi.service.dubboService.impl.otd;

import com.yunxi.core.util.DateUtil;
import com.yunxi.core.web.ApiResult;
import com.yunxi.core.web.LoginInfo;
import com.yunxi.core.web.ResponseUtil;
import com.yunxi.otd.pojo.OtdProduceCalendarVO;
import com.yunxi.otd.pojo.OtdProduceCalendarWeekVO;
import com.yunxi.otd.pojo.OtdWeekColumnHeadVO;
import com.yunxi.otd.service.OtdProduceCalendarDubboService;
import com.yunxi.service.localService.otd.OtdProduceCalendarService;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.util.CollectionUtils;

/**
 * <p>
 * 生产工作日历 服务实现类
 * </p>
 *
 * @author XUQI
 * @since 2019-07-02
 */
public class OtdProduceCalendarDubboServiceImpl implements OtdProduceCalendarDubboService {

    @Autowired
    @Qualifier(value = "otdProduceCalendarServiceImpl")
    OtdProduceCalendarService otdProduceCalendarService;

    /**
     * 返回日历List (格式是前端要求)
     *
     * @param map 查询条件
     * @return List<List < OtdProduceCalendarVO>>
     */
    @Override
    public List<List<OtdProduceCalendarVO>> listByMap(Map<String, Object> map) {
        List<List<OtdProduceCalendarVO>> result = new ArrayList<>();
        // 查询往后6个月
        List<OtdProduceCalendarVO> calendarList = otdProduceCalendarService.listBy(map);
        if (CollectionUtils.isEmpty(calendarList)) {
            return result;
        }
        // Map<月份,日期集合>
        Map<String, List<OtdProduceCalendarVO>> calendarMap = new LinkedHashMap<>();
        calendarList.forEach(vo -> {
            String month = DateUtil.format(vo.getDay(), "MM");
            List<OtdProduceCalendarVO> list = calendarMap.get(month);
            if (CollectionUtils.isEmpty(list)) {
                list = new ArrayList<>();
            }
            list.add(vo);
            calendarMap.put(month, list);
        });
        // 将每月日期集合放入resultList中
        calendarMap.forEach((s, calendarListByMonth) -> result.add(calendarListByMonth));
        return result;
    }

    /**
     * 查询每月周数
     *
     * @param map 查询条件
     * @return List<List < OtdProduceCalendarVO>>
     */
    @Override
    public List<List<String>> weekListByMap(Map<String, Object> map) {
        List<List<String>> result = new ArrayList<>();
        // 查询每月包含周数
        List<OtdProduceCalendarWeekVO> calendarWeekVOS = otdProduceCalendarService.weekListByMap(map);

        // 按月分组
        Map<String, List<String>> weekMap = new LinkedHashMap<>();
        for (int i = 1; i <= calendarWeekVOS.size(); i++) {
            OtdProduceCalendarWeekVO calendarWeekVO = calendarWeekVOS.get(i - 1);
            List<String> list = weekMap.get(calendarWeekVO.getMonth());
            if (CollectionUtils.isEmpty(list)) {
                list = new ArrayList<>();
            }
            list.add(calendarWeekVO.getWeek());
            weekMap.put(calendarWeekVO.getMonth(), list);
        }
        weekMap.forEach((key, monthWeekList) -> {
            // 不满6周补齐6周
            if (monthWeekList.size() < 6) {
                String lastWeek = monthWeekList.get(monthWeekList.size() - 1);
                for (int j = 1; j <= 6 - monthWeekList.size(); j++) {
                    String week = String.valueOf((Integer.parseInt(lastWeek) + j));
                    monthWeekList.add(week.length() == 1 ? "0" + week : week);
                }
            }
            result.add(monthWeekList);
        });
        return result;
    }

    @Override
    public void save(OtdProduceCalendarVO vo, LoginInfo info) {
        otdProduceCalendarService.updateIsWorkByDay(vo.getDay(), vo.getIsWork(), info.getRealName());
    }

    /**
     * 根据年月查询非工作日
     * @param yearMonth 年月 201901
     * @return ApiResult data 日期集合
     */
    @Override
    public ApiResult queryNoWorkday(String yearMonth) {
        List<Integer> resultList = new ArrayList<>();
        List<Date> list = otdProduceCalendarService.findWorkDayByYearMonth(yearMonth, "0");
        if (!CollectionUtils.isEmpty(list)) {
            for (Date date : list) {
                resultList.add(Integer.valueOf(DateUtil.format(date, "d")));
            }
        }
        return ResponseUtil.ok(resultList);
    }

    /**
     * 根据年月查询周数 格式：01(2019-01-01~2019-01-07)
     *
     * @param yearMonth 年月
     * @return List<List < OtdProduceCalendarVO>>
     */
    @Override
    public ApiResult weekColumnHead(String yearMonth) {
        return otdProduceCalendarService.weekColumnHead(yearMonth);
    }

    /**
     * 根据选定周查询5周列头 格式：01(2019-01-01~2019-01-07)
     *
     * @param year 年
     * @param week 周
     * @return ApiResult
     */
    @Override
    public ApiResult weekColumnHeadByWeek(String year, String week) {
        return otdProduceCalendarService.weekColumnHeadByWeek(year,week);
    }
}
